<template>
  <a-modal
    :width="1000"
    style="top: 10px"
    :maskClosable="false"
    :title="mdl.dialogTitleMap[mdl.dialogStatus]"
    @ok="mdl.dialogStatus === 'create' ? createData() : updateData()"
    v-model="mdl.visible"
    :confirm-loading="loading"
  >
    <a-form-model :model="mdl.formData" :rules="rules">
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="课程名称"
        prop="courseName"
      >
        <a-input placeholder="" v-model="mdl.formData.courseName"/>
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="课程类型"
        prop="courseType"
      >
        <a-select v-model="mdl.formData.courseType">
          <a-select-option :value="item.id" v-for="item in typeList">{{item.name}}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="总积分"
        prop="integral"
      >
        <a-input-number v-model="mdl.formData.integral" :min="1"/>
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="课程详情"
      >
        <a-textarea :rows="4" v-model="mdl.formData.courseInfo"/>
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="所属法律法规"
      >
        <a-select mode="multiple"
                  placeholder="请选择法律法规"
                  @change="handleChange"
                  :value="flfgValueList"
                  allowClear>
          <a-select-option :key="item.id" v-for="(item, index) in flfgList">{{item.name}}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="封面图"
        prop="fileNo"
      >
        <coverMap :id.sync="mdl.formData.fileNo" @getImgFile="getImgFile" :msg.sync="mdl.formData.fileNo"/>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
/* eslint-disable */
  import coverMap from '../../homePages/coverMap'
import {couresInsert,couresup,courseedit} from '@/api/courseManage/courseManage'
import {infoDocIquery} from '@/api/invoice'
  export default {
      components: {
        coverMap
      },
      props: {
        /*   -------弹窗--------*/
        mdl: [Object,Array],
        typeList: [Object,Array],
      },
    data() {
      return {
        id: 0,
        form: this.$form.createForm(this),
        //PARTONE,
        param: {
          field: '' // 模拟接口接收的参数
        },
        plainOptions: [
          'df','sdf'
        ],
        flfgList: [],
        flfgValueList: [],
        infoDocIList: [],
        loading: false,
        rules: {
          courseName: [{ required: true, message: '课程名称不能为空', trigger: 'blur' }],
          courseType: [{ required: true, message: '课程类型不能为空', trigger: 'change' }],
          fileNo: [{ required: true, message: '封面图不能为空', trigger: 'blur' }],
          integral: [{ required: true, message: '课程积分不能为空', trigger: 'blur' }],
        }
      }
    },
    inject: ['upShow'],

    created() {
      this.getFlfgList()
    },
    watch: {
      visible(value) {
          if(value) {
            if(this.mdl.formData.id != null && this.mdl.formData.id != '') {
              this.getChapterList()
            }
          }

      }
    },
    computed: {
      visible() {
          return this.mdl.visible
      }
    },
    methods: {
      //获取章节及图片，编辑保存的时候需要
      getChapterList() {
        courseedit(this.mdl.formData.id).then(res=> {
          if(res.code == 200) {
            this.mdl.formData = res.data
            this.flfgValueList = []
            if(this.mdl.formData.docId!=null&&this.mdl.formData.docId!='') {
              this.flfgValueList = this.mdl.formData.docId.split(',')
            }
          }
        }).catch()
      },
      //查询法律法规名称
      getFlfgList() {
        const parameter = {
          _search: false,//是否分页查询
          pageSize: '',
          pageNumber: '',
          bgTm: "",
          data: {
            courseCount: "",
            id: '',
            intro: "",
            name: ''
          },
          endTm: "",
        }
        infoDocIquery(parameter).then(res => {
          res.data.records.forEach(e=> {
            e.id = e.id +''
          })
          this.flfgList = res.data.records
        }).catch(err => {

        })
      },
      //获取绑定法律法规
      handleChange(value) {
        this.flfgValueList = value
      },
      /**
      * 新增数据 couresInsert
      * */
      createData() {
            this.loading = true
            this.mdl.formData.infoDocIList = []
            this.flfgList.forEach(item=> {
                if(this.flfgValueList.indexOf(item.id) != -1) {
                  this.mdl.formData.infoDocIList.push(item)
                }
            })
            this.mdl.formData.courseChapterList = [];
            const parameter = this.mdl.formData
            couresInsert(parameter).then(res => {
              if(res.code === 200) {
                this.loading = false
                this.upShow(1,"新增成功")
                this.mdl.visible = false
                this.$emit("handleSearch");
              } else {
                this.loading = false
                this.upShow(2,res.msg)
              }
            }).catch(err => {
              this.loading = false
              console.log(err)
            })
      },
//      编辑
      updateData() {
          this.loading = true
          this.mdl.formData.infoDocIList = []
          this.flfgList.forEach(item=> {
            if(this.flfgValueList.indexOf(item.id) != -1) {
              this.mdl.formData.infoDocIList.push(item)
            }
          })
          const parameter = {
            courseInfo: this.mdl.formData.courseInfo,
            courseName: this.mdl.formData.courseName,
            courseType: this.mdl.formData.courseType,
          fileNo: this.mdl.formData.fileNo,
            id: this.mdl.formData.id,
          integral: this.mdl.formData.integral,
            infoDocIList: this.mdl.formData.infoDocIList,
            courseChapterList: []
          }
//          console.log("编辑", parameter)
          couresup(parameter).then(res => {
            if (res.code === 200) {
              this.loading = false
              this.upShow(1, "编辑成功")
              this.mdl.visible = false
              this.$emit("handleSearch");
            } else {
              this.loading = false
              this.upShow(2, res.msg)
            }
          }).catch(err => {
            this.loading = false
            console.log(err)
          })
      },
      // 获取上传的图片数据
      getImgFile(val) {
        if(val == '') {
          this.mdl.formData.fileNo = val;
        }else {
          this.mdl.formData.fileNo = val.fileNo;
        }
      }
    }
  }
</script>